using System;
using System.Xml;

namespace umlungu.engineX.webX {
	/// <summary>
	/// umlungu engineX web framework, site class.
	/// </summary>
	public class xmlX
	{
		#region Public methods
		/// <summary>Constructor</summary>
		public xmlX() {
		}
		/// <summary>
		/// Method description
		/// </summary>
		public static XmlElement AddElem(XmlDocument xmldoc, XmlNode node, string name, string value ) {
			XmlElement elem = (XmlElement) node.AppendChild(xmldoc.CreateElement(name));
			elem.InnerText = value;
			return(elem);
		}
		/// <summary>
		/// Method description
		/// </summary>
		public static XmlElement UpdElem(XmlDocument xmldoc, string ndname, string elname, string elvalue ) {
			XmlElement elem = (XmlElement) xmldoc.SelectSingleNode(String.Concat("//", ndname, "/" + elname));
			if (elem == null) {
				elem = (XmlElement) xmldoc.SelectSingleNode(String.Concat("//", ndname));
				if (elem == null)
					return(null);
				else
					elem = (XmlElement) elem.AppendChild(xmldoc.CreateElement(elname));
				}
			elem.InnerText = elvalue;
			return(elem);
		}
		/// <summary>
		/// Method description
		/// </summary>
		public static void AddAttr(XmlDocument xmldoc, XmlNode node, string name, string value ) {
			XmlElement elem = (XmlElement) node;
			elem.SetAttribute(name, (value == "" || value == null)? "-" : value);
		}
		/// <summary>
		/// Method description
		/// </summary>
		public static XmlElement AddElemAttr(XmlDocument xmldoc, XmlNode node, string elname, string elvalue, string atname, string atvalue) {
			XmlElement elem = (XmlElement) node.AppendChild(xmldoc.CreateElement(elname));
			elem.InnerText = elvalue;
			elem.SetAttribute(atname, atvalue);
			return(elem);
		}
		/// <summary>
		/// Method description
		/// </summary>
		// which =1(only elements) =2(only attributes) =3(both)
		public static XmlElement AddElemAttrNode(XmlDocument xmldoc, XmlNode node, string elname, int which) {
			XmlElement elem = xmldoc.CreateElement(elname);
			if (which == 1 || which == 3)
				foreach (XmlAttribute attr in node.Attributes)
					AddAttr(xmldoc, elem, attr.Name, attr.InnerText);
			if (which == 2 || which == 3)
				foreach (XmlNode nde in node.ChildNodes)
					if (nde.NodeType == XmlNodeType.Text)
						AddAttr(xmldoc, elem, "description", nde.InnerText);
					else
						AddAttr(xmldoc, elem, nde.Name, nde.InnerText);
			return(elem);
		}
		/// <summary>
		/// CloneNode:: clones a element to a new element where the element to be cloned is in another XmlDocument
		/// </summary>
		public static XmlElement CloneElement(XmlDocument xmldoc, XmlNode node) {
			if (node.NodeType == XmlNodeType.CDATA)
				return(CloneElement(xmldoc, node.InnerText));
			else if (node.NodeType != XmlNodeType.Comment)
				return(CloneElement(xmldoc, (XmlElement) node, true));
			else 
				return null;
		}
		public static XmlElement CloneElement(XmlDocument xmldoc, XmlElement elem) {
			return(CloneElement(xmldoc, elem, true));
		}
		public static XmlElement CloneElement(XmlDocument xmldoc, string htmlstr) {
			XmlElement newelem = xmldoc.CreateElement("html");
			newelem.InnerText = htmlstr;
			return newelem;
		}
		public static XmlElement CloneElement(XmlDocument xmldoc, XmlElement elem, bool deep) {
			XmlElement newelem = xmldoc.CreateElement(elem.Name);
			if (deep) {
				newelem.InnerXml = elem.InnerXml;
				foreach (XmlAttribute attr in elem.Attributes)
					newelem.SetAttribute(attr.Name, attr.Value);
			}
			return newelem;
		}
		/// <summary>
		/// RemoveAll:: removes the nodes in a nodelist from their parents
		/// </summary>
		public static void RemoveAll(XmlNodeList nodes) {
			foreach (XmlNode node in nodes)
				node.ParentNode.RemoveChild(node);
		}
		#endregion

		#region Protected methods
		#endregion

		#region Private methods
		#endregion
	}

}
		
